package org.mifos.boss.ui.controller;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.GroupDefinitionException;

import org.apache.log4j.Logger;
import org.mifos.boss.application.model.UserCredentials;
import org.mifos.boss.domainmodel.CSIndividualSavingsAccount;
import org.mifos.boss.domainmodel.CSLoanAccount;
import org.mifos.boss.domainmodel.CSSavingsAccount;
import org.mifos.boss.domainmodel.ClientCollectionData;
import org.mifos.boss.domainmodel.CollectionSheet;
import org.mifos.boss.service.BossDeliveryServiceInterface;
import org.mifos.boss.webservice.model.CenterDescription;
import org.mifos.boss.webservice.model.CollectionSheetCustomerLoanDto;
import org.mifos.boss.webservice.model.CollectionSheetCustomerSavingDto;
import org.mifos.boss.webservice.model.CollectionSheetDto;
import org.mifos.boss.webservice.model.CustomerHierarchy;
import org.mifos.boss.webservice.model.GroupDescription;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.mifos.boss.webservice.model.CollectionSheetCustomerDto;

@Controller
@RequestMapping("/bossDelivery")
public class BossDeliveryController extends AbstractController implements org.springframework.web.servlet.mvc.Controller {

	static Logger logger = Logger.getLogger(BossDeliveryController.class);

	@Autowired
	@Qualifier("bossDeliveryService")
	private BossDeliveryServiceInterface bossDeliveryService;   
	
	
	@RequestMapping("/getCustomerHierarchy")
	public ModelAndView retrieveCustomerHierarchy(HttpSession session){
		
//		UserCredentials user = (UserCredentials)session.getAttribute("currentUser");
		
		UserCredentials user = new UserCredentials();
		user.setJ_password("testmifos");
		user.setJ_username("officer");
		
		CustomerHierarchy cust = bossDeliveryService.retrieveCustomerHierarchy(user);
		
		//create center list
		List<CenterDescription> centers = cust.getCenters();
		List<String> centerList = new ArrayList<String>();
		for(CenterDescription c: centers){
			centerList.add(c.getDisplayName());
		}
		
		
		List<GroupDescription> groups= cust.getGroups();
		List<String> groupList = new ArrayList<String>();
		for(GroupDescription g: groups){
			groupList.add(g.getDisplayName());
		}
//		
		ModelAndView model = new ModelAndView();
		model.setViewName("bossDelivery");
//test code		
//		centerList.add("one");
//		centerList.add("two");
//		groupList.add("g1");
//		groupList.add("g2");
//		
//		model.addAttribute("centerList", centerList);
		model.addObject("groupList", groupList);
		model.addObject("centerList", centerList);
		
		
		return model;
	}
	
	@RequestMapping("/getCollection")
	public CollectionSheet retrieveCollectionSheet(Model model, HttpSession session){
		
//		UserCredentials user = (UserCredentials)session.getAttribute("currentUser");
		
		UserCredentials user = new UserCredentials();
		user.setJ_password("testmifos");
		user.setJ_username("officer");
		
		CollectionSheetDto collectionSheetDto = bossDeliveryService.retreiveCollectionSheet(user);
		
		CollectionSheet collectionSheet = bossDeliveryService.collectonSheetDto2CollectionSheet(collectionSheetDto);
		
		return collectionSheet;
		
	}


	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	
	
}
